package com.cv.framework.websocket.core.utils;

import com.cv.system.api.token.form.LoginUser;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;

/**
 * WebSocket 工具类
 *
 * @author Charles_XDXD
 */
public class WebSocketFrameworkUtil {

    public static final String LOGIN_USER_ATTR = "LOGIN_USER";

    /**
     * 设置当前用户
     *
     * @param loginUser  用户信息
     * @param attributes Session
     */
    public static void setLoginUser(LoginUser loginUser, Map<String, Object> attributes) {
        attributes.put(LOGIN_USER_ATTR, loginUser);
    }

    /**
     * 获取当前用户
     *
     * @return 当前用户
     */
    public static LoginUser getLoginUser(WebSocketSession session) {
        return (LoginUser) session.getAttributes().get(LOGIN_USER_ATTR);
    }

    /**
     * 获得当前用户的编号
     *
     * @return 用户编号
     */
    public static Long getLoginUserId(WebSocketSession session) {
        LoginUser loginUser = getLoginUser(session);
        return loginUser != null ? loginUser.getId() : null;
    }


}
